package com.wfh.share;

import com.tencent.tauth.TAuthView;
import com.tencent.tauth.TencentOpenAPI;
import com.tencent.tauth.TencentOpenHost;
import com.tencent.tauth.bean.OpenId;
import com.tencent.tauth.http.Callback;
import com.wfh.R;
import com.wfh.R.layout;
import com.wfh.R.menu;
import com.wfh.util.Util;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;

public class QQShare{
	public static final String APP_ID = "222222";// 替换为开发者的appkey，例如"1646212860";
	public static final String TAG = "qqsdk";
	private String scope = "get_user_info,get_user_profile,add_share,add_topic,list_album,upload_pic,add_album";// 授权范围
	private AuthReceiver receiver;
	private Context activity;
	private OnCompleteListner listener=null;
	public QQShare(Context context){
		this.activity=context;
	}
	
	public void setOnCompleteListener(OnCompleteListner listener){
		this.listener=listener;
	}
	
	
	public void authorize(){
		receiver = new AuthReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(TencentOpenHost.AUTH_BROADCAST);
		activity.registerReceiver(receiver, filter);

		Intent intent = new Intent(activity, TAuthView.class);
		intent.putExtra(TencentOpenHost.CLIENT_ID, APP_ID);
		intent.putExtra(TencentOpenHost.SCOPE, scope);
		intent.putExtra(TencentOpenHost.TARGET, "_self");
		intent.putExtra(TencentOpenHost.CALLBACK, "");
		activity.startActivity(intent);
	}

	public class AuthReceiver extends BroadcastReceiver {
		private static final String TAG = "AuthReceiver";

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle exts = intent.getExtras();
			String raw = exts.getString("raw");
			final String access_token = exts.getString("access_token");
			final String expires_in = exts.getString("expires_in");

			// 用access token 来获取open id
			TencentOpenAPI.openid(access_token, new Callback() {

				public void onCancel(int flag) {
					Log.i("flag", flag + "");
				}

				@Override
				public void onSuccess(final Object obj) {
					((Activity) activity).runOnUiThread(new Runnable() {
						@Override
						public void run() {
							Util.saveShare(activity, "qqopenid",
									((OpenId) obj).getOpenId());
							Util.saveShare(activity, "qqtoken",
									access_token);
							Util.saveShare(activity, "qqexpires",
									expires_in);
							Util.showToast(activity, "认证成功");
						}
					});
					
					if(listener!=null) listener.onComplete();
				}

				@Override
				public void onFail(int ret, final String msg) {
					((Activity) activity).runOnUiThread(new Runnable() {
						@Override
						public void run() {
							Util.showToast(activity, "认证失败");
						}
					});
				}
			});

		}
	}
	
	
	public void loginOut(){
		
	}

	public void unReceiver() {
		if (receiver != null) {
			activity.unregisterReceiver(receiver);
		}
	}
	

}
